package controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import service.SoilService;

import javax.annotation.Resource;
import java.util.*;

@Controller
public class SoilController {
    @Resource(name = "soilService")
    private SoilService soilService;

    @ResponseBody
    @RequestMapping("/getDataByKeywords")
    public List<Map<String, Object>> getDataByCityName(String city) {
        List<Map<String, Object>> dataList = soilService.getDataByCityName(city);
        System.out.println(dataList);
        return dataList;
    }

    /**
     * 通过经纬度查询采样点的所有样品
     * @param longitude 经度
     * @param latitude 纬度
     * @return
     */
    @ResponseBody
    @RequestMapping("/getDataByCoordinate")
    public List<Map<String, Object>> getDataByCoordinate(String longitude, String latitude) {
        List<Map<String, Object>> dataList = soilService.getDataByCoordinate(longitude, latitude);
        System.out.println(dataList);
        return dataList;
    }

    @ResponseBody
    @RequestMapping("/getDataByCoordinateAndDepth")
    public List<Map<String, Object>> getDataByCoordinateAndDepth(String longitude, String latitude, double depth) {
        List<Map<String, Object>> dataList = soilService.getDataByCoordinateAndDepth(longitude, latitude, depth);
        System.out.println(dataList);
        return dataList;
    }

    /**
     * 获取各城市采样点数量映射
     * @param originData_json 原始数据的json字符串
     * @return 各城市采样点数量映射
     */
    @ResponseBody
    @RequestMapping("/getGlobalData")
    public Map<String, Object> getGlobalData(@RequestParam("originData") String originData_json) {
        try {
            ObjectMapper om = new ObjectMapper();
            //解析前端传来的json字符串为List集合
            List<Map<String, Object>> originData = om.readValue(originData_json, List.class);
            return soilService.getGlobalData(originData);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 获取所有散点的值
     * @return 各个散点值的json对象字符串
     */
    @ResponseBody
    @RequestMapping("/getAllPoint")
    public Map<String, String[]> getAllPoint(){
        return soilService.getAllPoint();
    }
}